package com.xujf.sym.model.storage;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.xujf.sym.model.entity.User;

public final class LoginShared {

    private LoginShared() {
    }

    private static final String TAG = "LoginShared";

    private static final String KEY_ACCESS_TOKEN = "accessToken";
    private static final String KEY_ID = "id";
    private static final String KEY_AVATAR_URL = "avatarUrl";
    private static final String KEY_SCORE = "score";

    private static String accessToken;
    private static String id;
    private static String avatarUrl;
    private static Integer score;

    private static final String KEY_TOKEN = "token";
    private static final String KEY_AVATAR_URL_48 = "avatarUrl48";
    private static final String KEY_AVATAR_URL_210 = "avatarUrl210";
    private static final String KEY_LOGIN_PASSWORD = "loginPassword";
    private static final String KEY_LOGIN_NAME = "loginName";
    private static final String KEY_USER_POINT = "userPoint";

    private static String loginName;
    private static String token;
    private static String avatarUrl210;
    private static Integer point;

    public static void login(@NonNull Context context, @NonNull String userName, @NonNull String loginPassword, @NonNull String loginToken) {
        SharedWrapper sharedWrapper = SharedWrapper.with(context, TAG);
        sharedWrapper.setString(KEY_LOGIN_NAME, userName);
        sharedWrapper.setString(KEY_LOGIN_PASSWORD, loginPassword);
        sharedWrapper.setString(KEY_TOKEN, loginToken);
        loginName = userName;
        token = loginToken;
    }

    public static void update(@NonNull Context context, @NonNull User user) {
        SharedWrapper sharedWrapper = SharedWrapper.with(context, TAG);
        sharedWrapper.setString(KEY_LOGIN_NAME, user.getUserName());
        sharedWrapper.setString(KEY_AVATAR_URL_48, user.getUserAvatarURL48());
        sharedWrapper.setString(KEY_AVATAR_URL_210, user.getUserAvatarURL210());
        sharedWrapper.setInt(KEY_USER_POINT, user.getUserPoint());
        loginName = user.getUserName();
        avatarUrl210 = user.getUserAvatarURL210();
        point = user.getUserPoint();
    }

    public static void logout(@NonNull Context context) {
        SharedWrapper.with(context, TAG).clear();
        accessToken = null;
        id = null;
        loginName = null;
        avatarUrl = null;
        score = null;
    }

    public static String getId(@NonNull Context context) {
        if (TextUtils.isEmpty(id)) {
            id = SharedWrapper.with(context, TAG).getString(KEY_ID, null);
        }
        return id;
    }

    public static String getAvatarUrl210(@NonNull Context context) {
        if (TextUtils.isEmpty(avatarUrl210)) {
            avatarUrl210 = SharedWrapper.with(context, TAG).getString(KEY_AVATAR_URL_48, null);
        }
        return avatarUrl210;
    }

    public static String getLoginName(@NonNull Context context) {
        if (TextUtils.isEmpty(loginName)) {
            loginName = SharedWrapper.with(context, TAG).getString(KEY_LOGIN_NAME, null);
        }
        return loginName;
    }

    public static String getToken(@NonNull Context context) {
        if (TextUtils.isEmpty(token)) {
            token = SharedWrapper.with(context, TAG).getString(KEY_TOKEN, null);
        }
        return token;
    }


    public static int getPoint(@NonNull Context context) {
        point = SharedWrapper.with(context, TAG).getInt(KEY_USER_POINT, 0);
        return point;
    }

}
